﻿using Alimentador.Foods;
using System;
using System.Windows;
using System.Windows.Controls;

namespace Alimentador.Patient
{
    /// <summary>
    /// Interaction logic for StarValues.xaml
    /// </summary>
    public partial class StarValues : UserControl
    {
        /// <summary>
        /// Number of stars starting at 0 (zero)
        /// </summary>
        public static int MAX_STARS = 4;

        private Style m_fullStar = null;
        private Style m_emptyStar = null;
        private int m_currentValue = 0;
        private int m_maxValue;

        /// <summary>
        /// Constructor
        /// </summary>
        public StarValues()
        {
            InitializeComponent();
            SetInitValues();
            SetStarValues(0);
        }

        private void SetInitValues()
        {
            m_fullStar = (Style)FindResource("ButtonStarFull");
            m_emptyStar = (Style)FindResource("ButtonStarEmpty");

            SetMaxValue = MAX_STARS;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button s = sender as Button;
            if (s != null && s.Tag != null)
            {
                int value = 0;
                Int32.TryParse(s.Tag.ToString(), out value);
                SetStarValues(value);
                CurrentValue = value;
            }
        }

        public int CurrentValue
        {
            get
            {
                return m_currentValue;
            }
            set
            {
                m_currentValue = value;
            }
        }

        public int SetMaxValue
        {
            set
            {
                m_maxValue = value;

                OneStarBtn.IsEnabled =
                    TwoStarBtn.IsEnabled =
                    ThreeStarBtn.IsEnabled =
                    FourStarBtn.IsEnabled =
                    FiveStarBtn.IsEnabled = false;

                switch (m_maxValue)
                {
                    case 0:
                        {
                            OneStarBtn.IsEnabled = true;
                            break;
                        }
                    case 1:
                        {
                            OneStarBtn.IsEnabled = true;
                            TwoStarBtn.IsEnabled = true;
                            break;
                        }
                    case 2:
                        {
                            OneStarBtn.IsEnabled = true;
                            TwoStarBtn.IsEnabled = true;
                            ThreeStarBtn.IsEnabled = true;
                            break;
                        }
                    case 3:
                        {
                            OneStarBtn.IsEnabled = true;
                            TwoStarBtn.IsEnabled = true;
                            ThreeStarBtn.IsEnabled = true;
                            FourStarBtn.IsEnabled = true;
                            break;
                        }
                    case 4:
                    default:
                        {
                            OneStarBtn.IsEnabled = true;
                            TwoStarBtn.IsEnabled = true;
                            ThreeStarBtn.IsEnabled = true;
                            FourStarBtn.IsEnabled = true;
                            FiveStarBtn.IsEnabled = true;
                            break;
                        }
                }

                if (CurrentValue > m_maxValue)
                {
                    SetStarValues(m_maxValue);
                }
            }
            get
            {
                return m_maxValue;
            }
        }

        public void SetStarValues(DefaultValues.PatStarRatingValues value)
        {
            SetStarValues((int)value);
        }

        private void SetStarValues(int value)
        {
            OneStarBtn.Style = m_emptyStar;
            TwoStarBtn.Style = m_emptyStar;
            ThreeStarBtn.Style = m_emptyStar;
            FourStarBtn.Style = m_emptyStar;
            FiveStarBtn.Style = m_emptyStar;

            switch (value)
            {
                case 5:
                    {
                        StarLbl.Content = MainHelper.GetEnumDescription(DefaultValues.PatStarRatingValues.ActivityExtra);
                        OneStarBtn.Style =
                            TwoStarBtn.Style =
                            ThreeStarBtn.Style =
                            FourStarBtn.Style =
                            FiveStarBtn.Style = m_fullStar;
                        break;
                    }
                case 4:
                    {
                        StarLbl.Content = MainHelper.GetEnumDescription(DefaultValues.PatStarRatingValues.ActivityHigh);
                        OneStarBtn.Style =
                            TwoStarBtn.Style =
                            ThreeStarBtn.Style =
                            FourStarBtn.Style = m_fullStar;
                        break;
                    }
                case 3:
                    {
                        StarLbl.Content = MainHelper.GetEnumDescription(DefaultValues.PatStarRatingValues.ActivityModerate);
                        OneStarBtn.Style =
                            TwoStarBtn.Style =
                            ThreeStarBtn.Style = m_fullStar;
                        break;
                    }
                case 2:
                    {
                        StarLbl.Content = MainHelper.GetEnumDescription(DefaultValues.PatStarRatingValues.ActivityLight);
                        OneStarBtn.Style =
                            TwoStarBtn.Style = m_fullStar;
                        break;
                    }
                case 1:
                    {
                        StarLbl.Content = MainHelper.GetEnumDescription(DefaultValues.PatStarRatingValues.ActivitySedentary);
                        OneStarBtn.Style = m_fullStar;
                        break;
                    }
                default:
                    {
                        StarLbl.Content = MainHelper.GetEnumDescription(DefaultValues.PatStarRatingValues.ActivityNone);
                        break;
                    }
            }
        }
    }
}
